package com.fvff.sanctuary.dao.impl;

import com.fvff.sanctuary.dao.PersonDao;
import com.fvff.sanctuary.model.Person;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.Set;

/**
 * PersonDaoImpl
 */
@Repository
public final class PersonDaoImpl extends GenericDaoImpl<Person> implements PersonDao {

    public PersonDaoImpl() {
        super(Person.class);
    }

    public void remove(final Long id) {
        Person person = getIfExists(id);
        if (person != null) {
            getSession().delete(person);
        }
    }

    public void addActivity(final Long personId, final String activity) {
        Person p = (Person) getSession().load(Person.class, personId);
        Set<String> activities = p.getActivities();
        activities.add(activity);
        p.setActivities(activities);
        getSession().save(p);
    }

    public void removeActivity(final Long personId, final String activity) {
        Person p = (Person) getSession().load(Person.class, personId);
        Set<String> activities = p.getActivities();
        activities.remove(activity);
        p.setActivities(activities);
        getSession().save(p);
    }
}
